var Cheng={};
Cheng.option={};
Cheng.createChecked=function(arr, p) {
				var parent = Elf.controls.createElement("div","CheckboxList");
				var div = Elf.controls.createElement("label",{"for":"all"},parent);
				var allChecked = Elf.controls.createElement("input",{type:"checkbox",id:"all"},div);
				Elf.controls.createElement("span",{innerHTML:"全部"},div);
				var CheckList = [],label;
				arr.forEach(function(v,k){
					if(k%2==0){
						div = Elf.controls.createElement("div",parent);
					};
					label = Elf.controls.createElement("label",{"for":v[1]},div);
					CheckList[CheckList.length] = Elf.controls.createElement("input",{type:"checkbox",value:v[1],id:v[1]},label);
					Elf.controls.createElement("span",{innerHTML:v[0]},label);
				});
				var saveButton = Elf.controls.createElement("div","customForm_div_buttons_item slb",{innerHTML:"保存"},parent);
				Elf.xEvents.bind(allChecked,"click",function(){
					if(this.checked){
						CheckList.forEach(function(v){
							v.checked = true;
						});
					}else{
						CheckList.forEach(function(v){
							v.checked = false;
						});
					};
				});
				Elf.xEvents.bind(saveButton,"click",function(){
					var txt = [];
					var value = [];
					CheckList.forEach(function(v){
						if(v.checked){
							txt.push(v.nextElementSibling.innerHTML);
							value.push(v.value);
						};
					});
					p.nextElementSibling.value = txt.join(",");
					p.previousElementSibling.value = txt.length;
					var dialog = Elf.utils.closest(parent,".elf-dialog");
					Elf.components.dialog("close",dialog);
				});
				return parent;
};
Cheng.clearInputVlaue=function(form,arr){//清空表单内的值 //arr 保存不想清空的name值
	var x,e=form.elements,val ;
	for(x=0;x<e.length;x++){
		val = false ;
		if(e[x].type!="submit"||e[x].type!="button"){
			if(arr){
				arr.forEach(function(v,k){
					if(v == e[x].name){
						val = true;
					}
				});
			};
			if(!val){
				if(e[x].type==("checkbox"||"radio")){
					e[x].checked=false;
				}else if(e[x].type=="text"||e[x].type=="password"){
					e[x].value="";
				};
			}
		};
	}
};
Cheng.createFormInput=function(arr,n,className,data){//具有验证性的表单生成 arr ["控件前面字段名(*必填必须在头部),name值,正则,"其他特殊的东西","必须是element" ]
													// select 的option 可以自动生成 但是数据必须是Cheng.option[name]=data	
													//arr 拥有一个onClick 以及buttons数组 数组规定[{value:value,type:type||submit,handle:function(data){
													//	this 指向表单 
													//  data 是表单内所有具有name值input的序列化 
													//}}]
 var form_parent = Elf.controls({name:"div",className:className}),
	 form = Elf.controls({name:"form",className:"customForm"}),
 	 len = arr.length,
	 g = /^\*/,//监测必填项
	 div,label,em,span,DIV;
	 Elf.controls.appendTo(form,form_parent);
 function getFormData(){
		var e = form.elements,x,obj={};
		for(x=0;x<e.length;x++){
			if(!!e[x].name){
				obj[e[x].name] = e[x].value;		
			}
		};
		return obj;
};	 
			if(!arr[0].isEle){
 	 			div = Elf.controls.createElement("div","customForm_div",form);
			};
 	 for(var i=0;i<len;i++){
 	 	em = false;
 	 	if(arr[i].isEle){
	 	 			Elf.controls.createElement("div","customForm_div_Title",form,{innerHTML:"<span>"+arr[i].txt+"</span>"});
	 	 			div = Elf.controls.createElement("div","customForm_div",form);
 	 			continue;
 	 	};
 	 	if(div&&div.children.length===n){
 	 			div = Elf.controls.createElement("div","customForm_div",form);
 	 	};
 	 	label = Elf.controls.createElement("label",div,"customForm_div_label",{"for":arr[i][1]+"Form"});
// 	 	Elf.utils.css(label,{width:100/n+"%"})
 	 	if(g.test(arr[i][0])){
 	 		em = true;
 	 	};	
 	 	span=Elf.controls.createElement("span",label,em?"customForm_div_label_span em":"customForm_div_label_span",{innerHTML:em?"<em>"+arr[i][0][0]+"</em>"+arr[i][0].substr(1):arr[i][0]});
		switch(arr[i][3]){
			case "select":
   	 			Elf.controls.createElement("select",label,"customForm_div_label_select",{id:arr[i][1]+"Form",name:arr[i][1]});
			    Cheng.createFormOption(Cheng.option[arr[i][1]],form[arr[i][1]],1)	;
				break;
			case "textarea":
   	 			Elf.controls.createElement("textarea",label,"customForm_div_label_textarea",{id:arr[i][1]+"Form",name:arr[i][1]});
   	 			break;
			case "radio":
				DIV = Elf.controls.createElement("div",label);
				var TTTT = arr[i][1].split(",");
				label = Elf.controls.createElement("label","yylx",DIV);
				if(em){
					Elf.controls.createElement("input",{name:TTTT[0],type:"radio",required:"required"},label).value = "1";
				}else{
					Elf.controls.createElement("input",{name:TTTT[0],value:"1",type:"radio"},label).value = "1";
				};
				Elf.controls.createElement("span",{innerHTML:TTTT[1]||"是"},label);
				label = Elf.controls.createElement("label","yylx",DIV);
				Elf.controls.createElement("input",{name:TTTT[0],value:"0",type:"radio"},label).value="0";
				Elf.controls.createElement("span",{innerHTML:TTTT[2]||"否"},label);
   	 			break;
			default:
				if(typeof arr[i][3]=="object"){
					Elf.controls.appendTo(arr[i][3],label);
				}else{
	   	 			 Elf.controls.createElement("input",label,"customForm_div_label_input",{id:arr[i][1]+"Form",type:"text",name:arr[i][1]});
				}
		};
 	 	if(arr[i][2]){
 	 			form[arr[i][1]].setAttribute("pattern",arr[i][2]);
 	 	};
 	 	if(em){
 	 		if(arr[i][3]=="radio"){
//	 	 		form[arr[i][1]][0].setAttribute("required","required");
//	 	 		form.elements[i].setAttribute("required","required");
 	 		}else if(form[arr[i][1]]){
	 	 		form[arr[i][1]].setAttribute("required","required");
 	 		};
 	 	};
 	 	if(arr[i][3]&&arr[i][4]){
 	 		label.appendChild(arr[i][4]);
 	 	}
 	 };
 	 var Buttons =  Elf.controls.createElement("div",form,"customForm_div_buttons");
 	 var ute;
 	 if(arr["buttons"]){
 	 	for(var I=0;I<arr["buttons"].length;I++){
 	 		ute =  Elf.controls.createElement("input",Buttons,"customForm_div_buttons_item",{value:arr["buttons"][I].value,type:arr["buttons"][I].type||"submit"});
	 	 	ute.value="保存";
	 	 	ute.onclick=(function(I){
	 	 		return function(){
	 	 			form.onsubmit=function(e){
	 	 				e.preventDefault();
			 	 		arr["buttons"][I].handle.call(form,getFormData(),data);
	 	 			}
	 	 		} 
	 	 	})(I)	
 	 	}
 	 };
 	 if(data){
 	 	var x;
 	 	for(x in data){
 	 		if(form[x]){
   	 			if(form[x].tagName=="INPUT"){
   	 				if(form[x].type=="text"){
		 	 			form[x].value=data[x];
   	 				}else{
   	 					if(form[x].length){
   	 						for(var i=0;i<form[x].length;i++){
//	   	 						form[x][i].
   	 						};
   	 					}else{
   	 						if(data[x]=="1"){
	   	 						form[x].checked=true;
   	 						}
   	 					}
   	 				}
   	 			}else if(form[x].tagName=="SELECT"){
   	 				Cheng.setOpion(form[x],data[x]);
   	 			}
 	 		}
 	 	};
 	 };
 	 if(arr["onClick"]&&typeof arr["onClick"]=="function" ){
 	 		Elf.xEvents.bind(form,"click",function(e){
 	 			arr.onClick.call(form,e);
 	 		});
 		};	
 	 return form_parent;
};
Cheng.setOpion=function(el,v){//获取数据里的select对应的option
	var options = el.getElementsByTagName("option");
	var len = options.length;
	for(var i=0;i<len;i++){
		if(options[i].value==v){
			options[i].selected=true;
			break;
		};
	};
};
Cheng.createFormOption=function(arr,p,val){//创建option [[key,value]],父元素,是否有请选择
 	var len = arr.length;
 	if(val){
 		Elf.controls.createElement("option",p,"",{value:"",innerHTML:"请选择"});
 	};
 	 for(var i=0;i<len;i++){
 		Elf.controls.createElement("option",p,"",{value:arr[i][0],innerHTML:arr[i][1]});
 	 };
};
Cheng.ajax=function(obj){//{num:serviceNumber,args:{},callback:function(data){
//}}
		var Args = {
 		serviceModule:PCService.serviceModule,
 		serviceNumber:obj.num,
 		token:token,
 		args:obj.args,
 	};
 	if(obj.con){
	 	console.log(Args);
 	};
 	commonLogic.serviceCaller(Args,function(data){
 		if(data.flag == "true"){
 			var Data = data.result;
 			if(obj.con){
 				console.log(data,Args);
 			};
 			if(obj.callback){
		 		if(obj.callback(data)){
		 			Data = obj.callback(data);
		 		};
 			};
 			if(obj.grid){
 				Elf.components.grid.methods[obj.grid[0]](obj.grid[1],Data.result);
 			};
 			if(obj.paging){
 				Elf.components.paging.methods[obj.paging[0]](obj.paging[1],{total:Data.maxSize,currentPage:1})
 			}
 		}else{
 			Elf.components.toast({text:data.error});
 		}
 	});
};
Cheng.templates=function(url,func){
	Elf.components.ajax({
		context:"",
		url:url,
		beforeSend:function(){Elf.components.loading()},
		success:function(a){
			Elf.components.loading("close");
			func(a);
		}
	});
};
Cheng.createProvince=function(arr,p,d){
					var select1 = Elf.controls.createElement("select",{name:p.name1||"codeProvince",required:"required"},p);
					var select2 = Elf.controls.createElement("select",{name:p.name2||"codeCity",required:"required"},p);
					var inputs = Elf.controls.createElement("input",{type:"text",name:p.name3||"address",placeholder:"地址",required:"required"},p);
					Elf.controls.createElement("option",{innerHTML:"省",value:""},select1);
					Elf.controls.createElement("option",{innerHTML:"市",value:""},select2);
					arr.forEach(function(v){
						Elf.controls.createElement("option",{innerHTML:v.name,value:v.code},select1);
					});
					Elf.xEvents.bind(select1,"change",function(e){
						select2.length = 1;
						arr.forEach(function(v){
							if(select1.value == v.code){
								v.child.forEach(function(z){
									Elf.controls.createElement("option",{innerHTML:z.name,value:z.code},select2);
								});
							}
						});
					});
					if(d&&typeof d==="object"){
						for(var i=0;i<select1.options.length;i++){
							if(d[0]==select1.options[i].value){
											select1.options[i].selected=true;				
							};
							arr.forEach(function(v){
								if(d[0] == v.code){
									select2.length = 1;
									v.child.forEach(function(z){
										Elf.controls.createElement("option",{innerHTML:z.name,value:z.code},select2).selected=z.code == d[1]?true:false;
									});
								}
							});
						};
						inputs.value=d[2]||"";
					};
};
Cheng.fillSelect=function(d,el){
	if(d&&typeof d==="object"){
		for(var i=0;i<el[0].options.length;i++){
			if(d[0]==el[0].options[i].value){
							el[0].options[i].selected=true;				
			};
			staticDictionary.provinceData.forEach(function(v){
				if(d[0] == v.code){
					el[1].length = 1;
					v.child.forEach(function(z){
						Elf.controls.createElement("option",{innerHTML:z.name,value:z.code},el[1]).selected=z.code == d[1]?true:false;
					});
				}
			});
		}
	}
};
Cheng.getFormData=function(f){
				var obj = {};
				var i;
				f.elements = f.getElementsByTagName("input");
				var len = f.elements.length;
				for(i=0;i<len;i++){
					if(!f.elements[i].name){
						continue;
					};
					if(f.elements[i].type=="radio"){
						if(f.elements[i].checked){
							obj[f.elements[i].name] = f.elements[i].value;
						};
					}else if(f.elements[i].type=="checkbox"){
						if(f.elements[i].checked){
							if(obj[f.elements[i].name]){
								obj[f.elements[i].name].push(f.elements[i].value);
							}else{
								obj[f.elements[i].name]=[];
								obj[f.elements[i].name].push(f.elements[i].value);
							};
						};
					}else{
								obj[f.elements[i].name] = f.elements[i].value;
					};
				};
				return obj;
};
Cheng.disabledForm = function(form,arr){
	for(var iQ = 0; iQ < form.elements.length; iQ++) {
		if(form[iQ].type == "radio" ||form[iQ].type == "button" || form[iQ].type == "checkbox" || form[iQ].localName == "select" || form[iQ].type == "file") {
				form[iQ].disabled = true;
		} else {
				form[iQ].readOnly = true;
		};
	};
	if(!arr){
		return ;
	};
	for(var iQ = 0; iQ < form.elements.length; iQ++) {
		Elf.utils.each(arr,function(k,v){
			if(form[iQ].type == "radio" || form[iQ].type == "checkbox" || form[iQ].localName == "select" || form[iQ].type == "file") {
				if(form[iQ].name==v){
					form[iQ].disabled = false;
				};
			} else {
				if(form[iQ].name==v){
					form[iQ].readOnly = false;
				};
			};
		});
	};
};
Cheng.clearChildren=function(P){
				if(P.children.length==0){
					return ;
				};
				var item = P.children[P.children.length-1];
				if(item.remove){
					while(item){
						item.remove();
						item = P.children[P.children.length-1];
					};
				}else{
					while(item){
						P.removeChild(item);
						item = P.children[P.children.length-1];
					};
				};
			};
Cheng.getIndex=function(el,classname){
				var els = document.getElementsByClassName(classname);
				var index;
				Elf.utils.each(els,function(k,v){
					if(el==v){
						index = k+"";
					};
				});
				return index||null;
		};
Cheng.fillDataV2=function(el,data){
	var inputs = el.getElementsByTagName("input");
	Elf.utils.each(inputs,function(k,v){
		if(data[v.name]){
			if(v.type=="text"){
				v.value = data[v.name];
			}else if(v.type=="radio"){
				if(v.value==data[v.name]){
					v.click();
//					v.checked=true;
				};
			}else if(v.type=="checkbox"){
				var text = data[v.name].split(",");				
				Elf.utils.each(text,function(kk,vv){
					if(vv==v.value){
					v.click();
//						v.checked = true;
					};
				});
			};
		};
	});
};
Cheng.remove = function(el){
	if(!el){
		return ;
	};
	if(el.localName){
		el.parentElement.removeChild(el);
	}else{
		Elf.utils.each(el,function(k,v){
			el[0].parentElement.removeChild(el[0]);
		});
	};
};
Cheng.getIdByName=function(arr,value){
	var val ;
	Elf.utils.each(arr,function(k,v){
		if(v.name==value){
			val =  v.id;
		};
	});
	return val+"";
};
Cheng.FillData=function(form,data){//用数据填充容器 。 容器与数据的name值1 1对应
		Elf.utils.each(data,function(k,v){
                		if(form[k]){
                			if(form[k].localName=="input"){
	                			if(form[k].type=="text"){
	                				form[k].value = v;
	                			};
                			}else if(form[k][0].localName=="input"){
                				if(form[k][0].type=="radio"){
                					Elf.utils.each(form[k],function(k1,v1){
										if(v1.value==v){
											form[k][0].type="radio"
//											v1.checked= true;
											v1.click();
										}
                					})
                				}else if(form[k][0].type=="checkbox"){
                					var txet = v.split(",");
                					Elf.utils.each(txet,function(kk,vv){
                						Elf.utils.each(form[k],function(kkk,vvv){
                							if(vv==vvv.value){
//              								vvv.checked=true;
                								vvv.click();
                							}
                						})
                					})
                				};
                			}else if(form[k].localName=="select"){
                				var opts = form[k].getElementsByTagName("option");
                				Elf.utils.each(opts,function(k,vv){
									if(vv.value == v){
										vv.selected = true;
									};
                					
                				});
                			};
                		};
                	});
};
Cheng.radioRequire=function(form,num,len){
	for(var i=0;i<len;i++){
		Elf.xEvents.bind(form["field"+num][0],"click",function(){
			form["field"+num+1][0].required=true;
			form["field"+num+1][0].disabled=false;
			form["field"+num+1][1].disabled=false;
		});
		Elf.xEvents.bind(form["field"+num][1],"click",function(){
			form["field"+num+1][0].required=false;
			form["field"+num+1][0].checked=false;
			form["field"+num+1][1].checked=false;
			form["field"+num+1][0].disabled=true;
			form["field"+num+1][1].disabled=true;
		});
		num+=2;
	};
};
Cheng.bindRequired=function(form,name,bname){
	Elf.xEvents.bind(form[name][0],"click",function(e){
			Elf.utils.each(bname,function(k,v){
				form[v].required=true;
			});
	});
	Elf.xEvents.bind(form[name][1],"click",function(e){
			Elf.utils.each(bname,function(k,v){
				form[v].required=false;
			});
	});
};
Cheng.bindRequiredR=function(form,name,bname){
	Elf.xEvents.bind(form[name][0],"click",function(e){
			Elf.utils.each(bname,function(k,v){
				form[v][0].required=true;
			});
	});
	Elf.xEvents.bind(form[name][1],"click",function(e){
			Elf.utils.each(bname,function(k,v){
				form[v][0].required=false;
			});
	});
};
Cheng.clearDeBuff=function(form){
	var i,
	forms = form.elements,
	len = forms.length;
	for(i=0;i<len;i++){
		Elf.utils.removeAttr(forms[i],"required");
		Elf.utils.removeAttr(forms[i],"disabled");
		Elf.utils.removeAttr(forms[i],"readonly");
	};
};
Cheng.getFormDataV2=function(f){
				var obj = {};
				var form = f;
				var forms = form.elements;
				var len = forms.length;
				var obj = {};
				while(forms[len-1]){
					if(forms[len-1].name&&!forms[len-1].disabled){//没有name，禁用，或只读。
						var arrTxt = forms[len-1].name.split(".");
						if(arrTxt.length!=1){
							var dg ;
								arrTxt.forEach(function(v,k,o){
									if(k==0){
										dg = obj;
									};
									if(k!=o.length-1){
										if(!dg[v]){
											dg[v]={};
										};
											dg = dg[v];
									}else{
										switch(forms[len-1].type){
											case "radio":
												if(forms[len-1].checked){
													dg[v] = forms[len-1].value;
												};
											break;
											case "checkbox":
												if(forms[len-1].checked){
													if(dg[v]){
														dg[v].push(forms[len-1].value);
													}else{
														dg[v] = [forms[len-1].value];
													};
												};
											break;
											case "textarea":
													dg[v] = forms[len-1].value;
											break;
											case "select-one":
													dg[v] = forms[len-1].value;
											break;
											default:
													dg[v] = forms[len-1].value;
											;
										}
									};
								});
							dg = null;
						}else{
							switch(forms[len-1].type){
								case "radio":
									if(forms[len-1].checked){
										obj[arrTxt[0]] = forms[len-1].value;
									};
								break;
								case "checkbox":
									if(forms[len-1].checked){
										if(obj[arrTxt[0]]){
											obj[arrTxt[0]].push(forms[len-1].value);
										}else{
											obj[arrTxt[0]] = [forms[len-1].value];
										};
									};
								break;
								case "textarea":
										obj[arrTxt[0]] = forms[len-1].value;
								break;
								case "select-one":
										obj[arrTxt[0]] = forms[len-1].value;
								break;
								default:
										obj[arrTxt[0]] = forms[len-1].value;
								;
							}
							
							
							
						};
						
						
						
					};
						len--;
				};
				return obj;
};